package com.z1.core;

import java.io.File;

import org.apache.log4j.Logger;

import com.z1.core.exception.Z1Exception;

/**
 * @Description 系统路径获取类
 * @Create Date:Oct 28,2014
 * @author Dean Huang
 *
 */
public class PathUtil {
	
	private static final Logger logger = Logger.getLogger(PathUtil.class);
	
	//系统目录
	private static File SYS_PATH = null;
	//应用程序目录
	private static File APP_PATH = null;
	//脚本数据目录
	private static File SBIN_PATH = null;
	//配置数据目录
	private static File CONF_PATH = null;
	//本地数据目录
	private static File DATA_PATH = null;
	//JDK资源目录
	private static File JDK_PATH = null;
	//类库数据目录
	private static File LIB_PATH = null;
	//门户类库数据目录
	private static File PORTAL_LIB_PATH = null;
	//日志数据目录
	private static File LOG_PATH = null;
	//第三方插件目录
	private static File PLUGINS_PATH = null;
	
	//APP_HOME
	private static String appHome = System.getenv("APP_HOME");
	
	static {
		try {
			if (null == appHome) {
				appHome = new File("").getCanonicalPath();
				logger.warn("APP_HOME not found, auto set APP_HOME is " + appHome);
			}
			System.out.println("APP_HOME is " + appHome);
			
			//SYS_HOME
			String sysHome = System.getenv("SYS_HOME");
			if (null == sysHome) {
				SYS_PATH = new File(appHome).getCanonicalFile();
			}
			else {
				SYS_PATH = new File(sysHome).getCanonicalFile();
			}
			System.out.println("SYS_HOME is " + sysHome);
			
			//JAVA_HOME
			String javaHome = System.getenv("JAVA_HOME");
			if (null == javaHome) {
				JDK_PATH = new File(SYS_PATH, "jdk");
			}
			else {
				JDK_PATH = new File(javaHome).getCanonicalFile();
			}
			System.out.println("JAVA_HOME is " + javaHome);
			
			load();
		}
		catch (Throwable e) {
			throw new Z1Exception("PathUtil init err", e);
		}
	}
	
	public static void load() {
		try {
			APP_PATH = new File(appHome).getCanonicalFile();
			//脚本数据目录
			SBIN_PATH = new File(APP_PATH, "sbin");
			//配置数据目录
			CONF_PATH = new File(APP_PATH, "config");
			//本地数据目录
			DATA_PATH = new File(APP_PATH, "data");
			//日志目录
			LOG_PATH = new File(APP_PATH, "log");
			//类库数据目录
			LIB_PATH = new File(APP_PATH, "lib");
			//门户类库数据目录
			PORTAL_LIB_PATH = new File(APP_PATH, "portal-lib");
			//第三方插件目录
			PLUGINS_PATH = new File(APP_PATH, "plugins");
		}
		catch (Throwable e) {
			throw new Z1Exception(e);
		}
	}
	
	public static File getSysDir() {
		return SYS_PATH;
	}
	public static String getSysPath() {
		return SYS_PATH.getAbsolutePath();
	}
	
	public static File getAppDir() {
		return APP_PATH;
	}
	public static String getAppPath() {
		return APP_PATH.getAbsolutePath();
	}
	
	public static File getConfDir() {
		return CONF_PATH;
	}
	public static String getConfPath() {
		return getConfDir().getAbsolutePath();
	}
	
	public static File getDataDir() {
		return DATA_PATH;
	}
	public static String getDataPath() {
		return getDataDir().getAbsolutePath();
	}
	
	public static File getJdkDir() {
		return JDK_PATH;
	}
	public static String getJdkPath() {
		return getJdkDir().getAbsolutePath();
	}
	
	public static File getLibDir() {
		return LIB_PATH;
	}
	public static String getLibPath() {
		return getLibDir().getAbsolutePath();
	}
	
	public static File getPortalLibDir() {
		return PORTAL_LIB_PATH;
	}
	public static String getPortalLibPath() {
		return getPortalLibDir().getAbsolutePath();
	}
	
	public static File getLogDir() {
		return LOG_PATH;
	}
	public static String getLogPath() {
		return getLogDir().getAbsolutePath();
	}
	
	public static File getPluginsDir() {
		return PLUGINS_PATH;
	}
	public static String getPluginsPath() {
		return getPluginsDir().getAbsolutePath();
	}
	
	public static File getSbinDir() {
		return SBIN_PATH;
	}
	public static String getSbinPath() {
		return getSbinDir().getAbsolutePath();
	}
	
}
